package cn.jinjimi.app.main.adapter.recycler;

import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roy
 * 数据管理逻辑层
 * Date: 15/9/21
 * @author zhenhua
 */
public class MyBaseRecyclerDataAdapter<E, VH extends RecyclerView.ViewHolder> extends MyBaseRecyclerFrameAdapter<E, VH> {
    private Object mLock = new Object();
    protected List<E> mItems = new ArrayList<E>();

    @Override
    public void setItems(List<E> items) {
        if (items != null) {
            mItems.clear();
            mItems.addAll(items);
        } else {
            if (mItems != null) {
                mItems.clear();
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public void addItems(List<E> items) {
        synchronized (mLock) {
            if (items != null && !items.isEmpty()) {
                if (mItems == null) {
                    mItems = new ArrayList<E>();
                }
                mItems.addAll(items);
                notifyDataSetChanged();
//                notifyItemRangeInserted(getItemCount(), items.size());
            }
        }
    }

    @Override
    public void addItem(E item) {
        synchronized (mLock) {
            if (item != null) {
                if (mItems == null) {
                    mItems = new ArrayList<E>();
                }
                mItems.add(item);
                notifyItemInserted(getItemCount());
            }
        }
    }

    @Override
    public List<E> getItems() {
        return mItems;
    }

    @Override
    public E getItem(int position) {
        return mItems != null ? mItems.get(position) : null;
    }

    @Override
    public void insert(E item, int index) {
        synchronized (mLock) {
            if (getItems() != null) {
                getItems().add(index, item);
                notifyItemInserted(index);
            }
        }
    }

    @Override
    public void remove(E item) {
        synchronized (mLock) {
            if (getItems() != null) {
                getItems().remove(item);
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public void remove(int position) {
        synchronized (mLock) {
            if (getItems() != null) {
                getItems().remove(position);
                notifyItemRemoved(position);
            }
        }
    }

    @Override
    public void addItemInHead(E item) {
        synchronized (mLock) {
            if (item != null) {
                if (mItems == null) {
                    mItems = new ArrayList<E>();
                }
                mItems.add(0, item);
                notifyItemInserted(0);
            }
        }
    }

    @Override
    public void addItemsInHead(List<E> items) {
        synchronized (mLock) {
            if (items != null && !items.isEmpty()) {
                if (mItems == null) {
                    mItems = new ArrayList<E>();
                }
                mItems.addAll(0, items);
                notifyItemRangeInserted(0, items.size());
            }
        }
    }

    @Override
    public int getItemCount() {
        return mItems != null ? mItems.size() : 0;
    }

    @Override
    public void clearItems() {
        if (mItems != null) {
            mItems.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public void updateItem(int position, E item) {
        synchronized (mLock) {
            if (mItems != null && mItems.size() > position) {
                mItems.set(position, item);
                notifyDataSetChanged();
            }
        }
    }

    /*****************
     * 对于拥有下拉刷新等HeadView的RecyclerView使用以下方法
     ****************/
    public void remove(int position, int headViewCount) {
        synchronized (mLock) {
            if (getItems() != null) {
                getItems().remove(position);
                notifyItemRemoved(position + headViewCount);
            }
        }
    }

    public void insert(E item, int index, int headViewCount) {
        synchronized (mLock) {
            if (getItems() != null) {
                getItems().add(index, item);
                notifyItemInserted(index + headViewCount);
            }
        }
    }

    public void insert(List<E> items, int index, int headViewCount) {
        synchronized (mLock) {
            if (items != null && !items.isEmpty()) {
                if (mItems == null) {
                    mItems = new ArrayList<E>();
                }
                mItems.addAll(index, items);
                notifyItemRangeInserted(index + headViewCount, items.size());
            }
        }
    }

    public void update(int position, E item, int headViewCount) {
        synchronized (mLock) {
            if (getItems() != null && getItems().size() > position) {
                getItems().set(position, item);
                notifyItemChanged(position + headViewCount);
            }
        }
    }

    public boolean isEmpty() {
        return mItems.isEmpty();
    }
}
